package com.aco.fatureapp.activity;

import roboguice.activity.RoboActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.aco.fatureapp.model.Leiturista;
import com.aco.fatureapp.service.SrvAccess;
import com.aco.fatureapp.utilitary.TypefaceUtil;
import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;

/**
 *
 *
 */
public class BaseActivity extends RoboActivity implements ValidationListener {

	protected Leiturista usuarioLogado = null;

	protected Validator validator = null;

	/*
	 *
	 * (non-Javadoc)
	 *
	 * @see roboguice.activity.RoboActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		TypefaceUtil.overrideFont(getApplicationContext(), "SERIF",
				"fonts/VarelaRound-Regular.ttf");

		if (usuarioLogado == null) {
			usuarioLogado = SrvAccess.getUsuarioLogado(this,
					Leiturista.LEITURISTA);
		}
	}

	/*
	 *
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onPostCreate(android.os.Bundle)
	 */
	@Override
	protected void onPostCreate(final Bundle savedInstanceState) {
		/*
		 * Register validation.
		 */
		validator = new Validator(this);
		validator.setValidationListener(this);

		super.onPostCreate(savedInstanceState);
	}

	/*
	 *
	 * (non-Javadoc)
	 *
	 * @see
	 * com.mobsandgeeks.saripaar.Validator.ValidationListener#onValidationSucceeded
	 * ()
	 */
	@Override
	public void onValidationSucceeded() {
		// Implementes your appllication. When going through the validation.
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.mobsandgeeks.saripaar.Validator.ValidationListener#onValidationFailed
	 * (android.view.View, com.mobsandgeeks.saripaar.Rule)
	 */
	@Override
	public void onValidationFailed(final View failedView,
			final Rule<?> failedRule) {
		final String message = failedRule.getFailureMessage();

		if (failedView instanceof EditText) {
			failedView.requestFocus();
			((EditText) failedView).setError(message);
		}
		else {
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * Evento sair padrão, finaliza a atividade em execução.
	 *
	 * @return
	 */
	protected OnClickListener close() {
		return new OnClickListener() {
			@Override
			public void onClick(final View v) {
				finish();
			}
		};
	}

	/**
	 *
	 * @param btnPes
	 * @param btnCls
	 * @param campo
	 */
	protected void configureButtonClean(final Button btnPes,
			final Button btnCls, final EditText campo) {
		btnCls.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				campo.setText(null);
				btnCls.setVisibility(View.GONE);
				btnPes.setVisibility(View.VISIBLE);
			}
		});
	}

	/**
	 * @param enabled
	 * @param views
	 */
	protected void enabled(final Boolean enabled, final View... views) {
		for (int i = 0; i < views.length; i++) {
			views[i].setEnabled(enabled);
		}
	}

	/**
	 * @param edits
	 */
	protected void limpar(final EditText... edits) {
		for (int i = 0; i < edits.length; i++) {
			edits[i].setText("");
		}
	}

	/**
	 * @param images
	 */
	protected void limpar(final ImageView... images) {
		for (int i = 0; i < images.length; i++) {
			images[i].setImageBitmap(null);
		}
	}

	/**
	 * @param checks
	 */
	protected void limpar(final CheckBox... checks) {
		for (int i = 0; i < checks.length; i++) {
			checks[i].setChecked(false);
		}
	}

	/**
	 * Localizar o valor de uma variável configurada no String.xml.
	 *
	 * @param aString
	 *            Chave da String a ser pesquisada.
	 * @return Valor encontrado.
	 */
	protected String getStringResourceByName(String aString) {
		String packageName = getPackageName();

		int resId = getResources()
				.getIdentifier(aString, "string", packageName);

		if (resId == 0) {
			return aString;
		}

		return getString(resId);
	}

}
